﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace numerator
{
    class Program
    {
        static void Main(string[] args)
        {
            string file_in = "";
            string file_out = "";

            /*
             * try
            {  file_in = args[0];   }
            catch
            {
                Console.WriteLine("input file name required");
                return;
            }

            try
            { file_out = args[1]; }
            catch
            {
                Console.WriteLine("output file name required");
                return;
            }
            */
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();

            foreach (FileInfo fi in Files)
            {
                XmlDocument mydoc = null;
                Console.WriteLine("Processing " + fi.Name);
                try
                {
                    mydoc = new XmlDocument();
                    mydoc.Load(fi.FullName);
                }
                catch
                {
                    Console.WriteLine("file invalid");
                    return;
                }
              

                XmlNodeList paragraphs = mydoc.SelectNodes("//p");
                Console.WriteLine(paragraphs.Count.ToString() + " paragraphs found");
                for (int i = 0; i < paragraphs.Count; i++)
                {

                    XmlNodeList words = paragraphs[i].ChildNodes;
                    XmlAttribute attr = mydoc.CreateAttribute("sc");
                    attr.InnerText = words.Count.ToString();
                    paragraphs[i].Attributes.Append(attr);
                    for (int j = 0; j < words.Count; j++)
                    {
                        XmlAttribute attr_pos = mydoc.CreateAttribute("sn");
                        attr_pos.InnerText = (j + 1).ToString();
                        words[j].Attributes.RemoveAll();
                        words[j].Attributes.Append(attr_pos);

                    }
                }
                file_out = "./out/" + fi.Name;
                mydoc.Save(file_out);
            } 
        }
    }
}
